import { notification } from 'antd/lib/index';
import {fetchProjectFileTreeApi,addFileInfoApi,modifyFileInfoApi,fetchFileInfoApi,delFileInfoApi} from '../services/project';

export default {
  namespace: 'project',
  state: {
    projectTree: [],
    entityInfoObj: {},
  },

  effects: {
    *queryEntityPageList({ payload, callback }, { call, put }) {
      const response = yield call(fetchProjectFileTreeApi, payload);
      if (callback) callback(response);
      const entityPageResult =  eval(response.data);

      yield put({
        type: 'changeEntityPageList',
        payload: { code: response.code, entityPageResult },
      });
    },

    *detailEntity({ payload }, { call, put }) {
      const response = yield call(fetchFileInfoApi, payload);

      if (response.code === 10000) {
        yield put({
          type: 'changeDetailEntity',
          payload: response.data,
        });
      }
    },

    *updateEntity({ payload, callback }, { call }) {
       let response = yield call(modifyFileInfoApi, payload);

      if (response.code === 10000) {
        notification.success({
          message: '数据提交成功!',
        });
        if (callback) callback(response);
      }
    },

    *addEntity({ payload, callback }, { call }) {
      let response = yield call(addFileInfoApi, payload);

      if (response.code === 10000) {
        notification.success({
          message: '数据提交成功!',
        });
        if (callback) callback(response);
      }
    },

    *removeEntity({ payload, callback }, { call }) {
      const response = yield call(delFileInfoApi, payload);
      if (response.code === 10000) {
        notification.success({
          message: '删除成功！',
        });
        if (callback) callback(response);
      }
    },
  },

  reducers: {
    changeEntityPageList(state, { payload }) {
      return {
        ...state,
        projectTree: payload.entityPageResult,
      };
    },
    changeDetailEntity(state, { payload }) {
      return {
        ...state,
        entityInfoObj: payload,
      };
    },
  },
};
